
package character_11_;

import java.net.URL;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author Administrator
 */
public class Character_11_ {

    private static ArrayList2<String> sList = new ArrayList2<String>();
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception{
       
        Clerk2 clerk = new Clerk2();
        new Thread(new Producer(clerk)).start();
        new Thread(new Consumer(clerk)).start();
        
        Resource res1 = new Resource("zs");
        Resource res2 = new Resource("ls");
        
        Thread thread1 = new Thread(() -> {
        
            for (int i = 0; i < 10; i++) {
                res1.cooperate(res2);
            }
        });
        
        Thread thread2 = new Thread(() -> {
        
            for (int i = 0; i < 10; i++) {
                res2.cooperate(res1);
            }
            System.out.println("" + Thread.currentThread().getThreadGroup().getName());
        });
        
        thread1.start();
        thread2.start();
        
        System.out.println("" + Thread.currentThread().getThreadGroup().getName());
        
        String url = "https://image.baidu.com/search/index?tn=baiduimage&word=gril";

        URL[] urls = {
//            new URL("http://openhome.cc/Gossip/Encoding/"),
//            new URL("http://openhome.cc/Gossip/Scala/"),
//            new URL("http://openhome.cc/Gossip/JavaScript/"), 
//            new URL("http://openhome.cc/Gossip/Python/") 
            new URL(url)
        };
        
        String[] fileNames = {
        "test.html"
            //"Encoding.html", "Scala.html", "JavaScript.html", "Python.html"
        };
        
        ExecutorService executorService = Executors.newCachedThreadPool();
        new Pages(urls, fileNames, executorService).download();
        executorService.shutdown();//会在指定的Runnable完成后关闭ExecutorService
        
        //((Character_11_)null).testMethod();
        
        sList.add("haha");
        System.out.println("" + sList.get(0) + sList.size());
        
//        FutureTask<Long> the30FibFuture = new FutureTask<>(() -> fibonacci(30));
//        
//        System.out.println("输出第30个费式数");
//        new Thread(the30FibFuture).start();
//        while (!the30FibFuture.isDone()) {            
//            System.out.println("先执行其他事情");
//        }
//        System.out.println("第30个费式数是：" + the30FibFuture.get());
        
        ExecutorService service = Executors.newCachedThreadPool();
        Future<Long> the30FibFuture = service.submit(() -> fibonacci(30));
        System.out.println("输出第30个费式数");
        while (!the30FibFuture.isDone()) {            
            System.out.println("先执行其他事情");
        }
        System.out.println("第30个费式数是：" + the30FibFuture.get());
        
        ScheduledExecutorService service1 = Executors.newSingleThreadScheduledExecutor();
        service1.scheduleWithFixedDelay(() -> {
            System.out.println(new java.util.Date());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, 2000, 1000, TimeUnit.MICROSECONDS);
        
        Fibonacci fibonacci = new Fibonacci(45);
        ForkJoinPool pool = new ForkJoinPool();
        System.out.println(pool.invoke(fibonacci));
        
        BlockingQueue queue = new ArrayBlockingQueue(1);
        new Thread(new Producer3(queue)).start();
        new Thread(new Consumer3(queue)).start();
    }
    
    
    static long fibonacci(long n){
    
        if (n <= 1) {
            return n;
        }
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
    
    private static void testMethod() {
        System.out.println("testMethod");
        
    }
    
}
